package com.jy.appmarket.screenlock.adapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jy.appmarket.R;
import com.jy.appmarket.adapter.SimpleBaseAdapter;
import com.jy.appmarket.screenlock.model.MessageModel;

/**
 * ��˵����
 * 
 * @author jie.li
 * @date 2015-4-7
 * @version 1.0
 */
public class MessageAdapter extends SimpleBaseAdapter<MessageModel> {
	private Context mContext;

	public MessageAdapter(Context context) {
		super(context);
		mContext = context;
	}

	public MessageAdapter(Context context, List<MessageModel> list) {
		super(context, list);
		mContext = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = getInflater().inflate(R.layout.message_item, parent, false);
			holder = new ViewHolder();
			holder.icon = convertView.findViewById(R.id.imageView1);
			holder.appName = convertView.findViewById(R.id.app_name);
			holder.desc = convertView.findViewById(R.id.textView1);
			holder.time = convertView.findViewById(R.id.textView2);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		MessageModel model = mList.get(position);
		if (model.getAppName() != null && "toolbox_phone".equals(model.getAppName())) {
			holder.appName.setText("电话");
			holder.icon.setPadding(10, 10, 10, 10);
			holder.icon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.missed_calls));
		} else if (model.getAppName() != null && "toolbox_sms".equals(model.getAppName())) {
			holder.appName.setText("短信");
			holder.icon.setPadding(10, 10, 10, 10);
			holder.icon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.unread_sms));
		} else {
			try {
				holder.icon.setPadding(0, 0, 0, 0);
				PackageManager pm = mContext.getPackageManager();
				ApplicationInfo appInfo = pm.getApplicationInfo(model.getAppPkg(), 0);
				holder.appName.setText(appInfo.loadLabel(pm).toString());
				holder.icon.setImageDrawable(appInfo.loadIcon(mContext.getPackageManager()));
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
		}
		holder.desc.setText(model.getContent());
		holder.time.setText(longTime(model.getStartTime()));

		return convertView;
	}

	private String longTime(long time) {
		long now = System.currentTimeMillis();

		long diff = now - time;
		diff = diff / 1000;// 秒
		String format = "yyyy-MM-dd HH:mm:ss";

		if (diff < 0) {
			return dateLongToString(time, format);
		}

		if (diff < 30) { // 30秒
			return "刚刚";
		}

		if (diff < 60) {
			return String.format("%s秒前", diff);
		}

		if (diff < 3600) {
			return String.format("%s分钟前", diff / 60);
		}
		// 获取今天凌晨时间
		long todayStart = getMoring(new Date()).getTime();

		if (time >= todayStart) {// 今天
			return String.format("%s小时前", diff / 3600);
		}

		if (time < todayStart && time >= todayStart - 86400000) {
			return "昨天 " + dateLongToString(time, "HH:mm");
		}

		return dateLongToString(time, format);
	}

	// 获取今天凌晨的时间
	private static Date getMoring(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	public static String dateLongToString(long time) {
		return dateLongToString(time, null);
	}

	public static String dateLongToString(long time, String format) {
		if (time <= 0) {
			return "Empty";
		}
		SimpleDateFormat format2 = new SimpleDateFormat(format);
		String dateString = format2.format(new Date(time));
		return dateString;
	}

	private static final class ViewHolder {
		ImageView icon;
		TextView appName;
		TextView desc;
		TextView time;
	}

	public void remove(MessageModel item) {
		mList.remove(item);
		notifyDataSetChanged();
	}

}
